package org.boyan.trans;

import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.boyan.entity.query.QueryInterface;

import java.util.ArrayList;
import java.util.List;

//解析excel文件中的原始接口数据
public class ExcelToQueryInterface {

    //解析excel文件中输入项
    public static List<QueryInterface> getInput(Sheet sheet){
        List<QueryInterface> inputList=new ArrayList<>();
        try{
            int inputRow=getInputRow(sheet);
            int outputRow=getOutputRow(sheet);
            if(inputRow==-1||outputRow==-1){
                throw new Exception("文件解析失败，是否上传错误?");
            }
            for(int i=inputRow+1;i<outputRow;i++){
                if("".equals(sheet.getRow(i).getCell(0).getStringCellValue())){
                    continue;
                }
                QueryInterface queryInterface=new QueryInterface();
                queryInterface.setEnName(sheet.getRow(i).getCell(0).getStringCellValue());
                queryInterface.setCnName(sheet.getRow(i).getCell(1).getStringCellValue());
                queryInterface.setDataType(sheet.getRow(i).getCell(2).getStringCellValue());
                String inputFlag=sheet.getRow(i).getCell(3).getStringCellValue();
                if("Y".equals(inputFlag)||"是".equals(inputFlag)){
                    queryInterface.setInputFlag(true);
                }else{
                    queryInterface.setInputFlag(false);
                }
                queryInterface.setQueryLogic(sheet.getRow(i).getCell(4).getStringCellValue());
                inputList.add(queryInterface);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return inputList;
    }

    //解析excel文件中输出项
    public static List<QueryInterface> getOutput(Sheet sheet){
        List<QueryInterface> outputList=new ArrayList<>();
        try{
            int arrayObjectRow=getArrayStartRow(sheet);
            int outputRow=getOutputRow(sheet);
            if(arrayObjectRow==-1||outputRow==-1){
                throw new Exception("文件解析失败，是否上传错误?");
            }
            for(int i=outputRow+1;i<arrayObjectRow;i++){
                if("".equals(sheet.getRow(i).getCell(0).getStringCellValue())){
                    continue;
                }
                QueryInterface queryInterface=new QueryInterface();
                queryInterface.setEnName(sheet.getRow(i).getCell(0).getStringCellValue());
                queryInterface.setCnName(sheet.getRow(i).getCell(1).getStringCellValue());
                queryInterface.setDataType(sheet.getRow(i).getCell(2).getStringCellValue());
                String inputFlag=sheet.getRow(i).getCell(3).getStringCellValue();
                if("Y".equals(inputFlag)||"是".equals(inputFlag)){
                    queryInterface.setInputFlag(true);
                }else{
                    queryInterface.setInputFlag(false);
                }
                queryInterface.setQueryLogic(sheet.getRow(i).getCell(4).getStringCellValue());
                outputList.add(queryInterface);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return outputList;
    }

    //解析excel文件中表对象
    public static List<QueryInterface> getArrayObject(Sheet sheet){
        List<QueryInterface> arrayObjectList=new ArrayList<>();
        try{
            int arrayStart=getArrayStartRow(sheet);
            int arrayEnd=getArrayEndRow(sheet);
            if(arrayStart==-1||arrayEnd==-1){
                throw new Exception("文件解析失败，是否上传错误?");
            }
            for(int i=arrayStart+1;i<arrayEnd;i++){
                if("".equals(sheet.getRow(i).getCell(0).getStringCellValue())){
                    continue;
                }
                QueryInterface queryInterface=new QueryInterface();
                queryInterface.setEnName(sheet.getRow(i).getCell(0).getStringCellValue());
                queryInterface.setCnName(sheet.getRow(i).getCell(1).getStringCellValue());
                queryInterface.setDataType(sheet.getRow(i).getCell(2).getStringCellValue());
                String inputFlag=sheet.getRow(i).getCell(3).getStringCellValue();
                if("Y".equals(inputFlag)||"是".equals(inputFlag)){
                    queryInterface.setInputFlag(true);
                }else{
                    queryInterface.setInputFlag(false);
                }
                queryInterface.setQueryLogic(sheet.getRow(i).getCell(4).getStringCellValue());
                arrayObjectList.add(queryInterface);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return arrayObjectList;
    }

    //获取输入项的起始row
    private static int getInputRow(Sheet sheet){
        for(Row row:sheet){
            if("输入".equals(row.getCell(0).getStringCellValue())){
                return row.getRowNum();
            }
        }
        return -1;
    }

    //获取输出项的起始row
    private static int getOutputRow(Sheet sheet){
        for(Row row:sheet){
            if("输出".equals(row.getCell(0).getStringCellValue())){
                return row.getRowNum();
            }
        }
        return -1;
    }

    //获取表对象的起始row
    private static int getArrayStartRow(Sheet sheet){
        for(Row row:sheet){
            if("start".equalsIgnoreCase(row.getCell(4).getStringCellValue())){
                return row.getRowNum();
            }
        }
        return -1;
    }

    //获取表对象的结束row
    private static int getArrayEndRow(Sheet sheet){
        for(Row row:sheet){
            if("end".equalsIgnoreCase(row.getCell(4).getStringCellValue())){
                return row.getRowNum();
            }
        }
        return -1;
    }
}
